package stax;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.util.Iterator;

public class TestStAXParser {

    public static void main(String[] args) {


        XMLInputFactory factory=XMLInputFactory.newInstance();

        try {
            //解析器
            XMLEventReader parser=factory.createXMLEventReader(TestStAXParser.class
                                                                             .getClassLoader()
                                                                             .getResourceAsStream("students.xml"));




            while(parser.hasNext()){
                XMLEvent event=parser.nextEvent();

//                System.out.println(event);

                if(event.isStartDocument()){
                    System.out.println("开始解析");
                }

                if(event.isEndDocument()){
                    System.out.println("解析结束");
                }

                if(event.isStartElement()){
                    StartElement startElement= event.asStartElement();
                    System.out.println(startElement.getName());

                    Iterator<Attribute> attributeIterator= startElement.getAttributes();

                    while(attributeIterator.hasNext()){
                        Attribute attribute=attributeIterator.next();
                        System.out.println(attribute.getName()+"="+attribute.getValue());
                    }
                }

                if(event.isEndElement()){

                }

                if(event.isAttribute()){

                }

                if(event.isCharacters()){
                    System.out.println(event.asCharacters());
                }

            }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}
